package com.weathereport.activity.adapter;

import java.util.List;

import com.weathereport.R;
import com.weathereport.entity.Forecast;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class LocalForecastAdapter extends BaseAdapter{

	private Context context;
	private List<Forecast> forecast;

	public LocalForecastAdapter(Context context, List<Forecast> forecast) {
		this.context = context;
		this.forecast = forecast;
	}
	
	public int getCount() {
		if (forecastIsNotNull()) {
			return forecast.size();
		}
		return 0;
	}

	public Object getItem(int position) {
		if (forecastIsNotNull()) {
			return forecast.get(position);
		}
		return null;
	}

	private boolean forecastIsNotNull() {
		if (forecast != null && !forecast.isEmpty()) {
			return true;
		}
		return false;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View arg1, ViewGroup arg2) {
		Forecast forecastLocal = forecast.get(position);
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v 					= inflater.inflate(R.layout.list_localforecast, null);		
		TextView date 			= (TextView) v.findViewById(R.id.date);
		TextView weather 		= (TextView) v.findViewById(R.id.weather);
		TextView tempMin 		= (TextView) v.findViewById(R.id.tempMin);
		TextView tempMax 		= (TextView) v.findViewById(R.id.tempMax);
				
		date.setText(forecastLocal.getDay().toString().trim());
		
		if (forecast.size() > 0) {
			weather.setText(forecastLocal.getWeather().toString().trim());
			tempMin.setText(forecastLocal.getLowTemp().toString().toString().trim());
			tempMin.setTextColor(setColor(Integer.parseInt(tempMin.getText().toString())));
			tempMax.setText(forecastLocal.getHighTemp().toString().toString().trim());
			tempMax.setTextColor(setColor(Integer.parseInt(tempMax.getText().toString())));
		}else {
			
			weather.setText(R.string.noData);
			tempMin.setText("-");			
			tempMax.setText("-");			
		}
		
		return v;
	}
	
	public int setColor(int color){
		
		int result = 0;
		
		if (color <= 5){
			
			result = Color.BLUE;
		}
		
		if (color >= 6 && color <=14){
			
			result = Color.rgb(135, 206, 250);
		}
		
		if (color >= 15 && color <= 20){
			
			result = Color.GREEN;
		}
		
		if (color >=21 && color <= 30){
			
			result = Color.rgb(255, 165, 0);
		}
		
		if (color >= 31){
			
			result = Color.RED;
		}
		
		return result;
	}
}
